let result = [];
export const ALIYUN_OSS = "http://syg-photo.oss-cn-shanghai.aliyuncs.com/";
export function _jump(res) {
  const sessionJump = JSON.parse(sessionStorage.getItem("jumpTrue"));
  sessionJump[res.replace("/", "")] = true;
  sessionStorage.setItem("jumpTrue", JSON.stringify(sessionJump));
}
export function _reduceArr1(arr) {
  const result = [];
  const translator = arr => {
    arr.map(v => {
      result.push({
        permissionName: v.permissionName,
        icon: v.icon,
        id: v.id,
        permissionType: v.permissionType,
        parentId: v.parentId,
        id: v.id,
        url: v.url
      });
      if (v.children) {
        translator(v.children);
      }
    });
  };
  translator(arr);
  return result;
}
export function _treeData(data) {
  const parents = data.filter(
    v => v.parentId === undefined || v.parentId === null
  );
  const children = data.filter(
    v => v.parentId !== undefined || v.parentId !== null
  );
  const translator = (parents, children) => {
    parents.forEach(parent => {
      children.forEach((current, index) => {
        if (current.parentId === parent.id) {
          const temp = JSON.parse(JSON.stringify(children));
          temp.splice(index, 1);
          translator([current], temp);
          typeof parent.children !== "undefined"
            ? parent.children.push(current)
            : (parent.children = [current]);
        }
      });
    });
  };
  translator(parents, children);
  return parents;
}
export function _currentChange(val, page, searchInfo) {
  // 传递当前值 分页信息，以及需要copy的对象
  page.pageNo = val;
  const result = {};
  result.page = JSON.parse(JSON.stringify(page));
  Object.assign(result, searchInfo);
  return result;
}
export function _deleteMethods(url, data, that) {
  return new Promise(resolve => {
    trueOrFalse(that, 0).then(() => {
      that
        .$post({
          url: url,
          data: {
            ids: [data.id ? data.id : data.tcId ? String(data.tcId) : ""]
          }
        })
        .then(() => {
          that.$message({
            message: "操作成功",
            type: "success"
          });
          resolve();
        });
    });
  });
}
export function _deleteMethods1(url, data, that) {
  return new Promise(resolve => {
    trueOrFalse(that, 0).then(() => {
      that
        .$post({
          url: url,
          data: {
            id: data.id
          }
        })
        .then(() => {
          that.$message({
            message: "操作成功",
            type: "success"
          });
          resolve();
        });
    });
  });
}
export function _searchInfo(res, page, copySear) {
  res === "no" ? "" : page ? (page.pageNo = 0) : "";
  const result = {};
  page ? (result.page = JSON.parse(JSON.stringify(page))) : "";
  Object.assign(result, copySear);
  return result;
}

export function changeTableHeight(that) {
  if(that.tableData === null){
    return false
  }
  that.$nextTick(() => {
    const emptyTable = [];
    for (var i = 0; i < 18 - that.tableData.length; i++) {
      emptyTable.push({});
    }
    that.tableData = [...that.tableData, ...emptyTable];
    console.log(window.innerHeight);
    that.pageHeight = window.innerHeight - 80;
  });
}
export function _changeTable(url, data, that, no) {
  that
    .$post({
      url: url,
      data: data
    })
    .then(res => {
      if (res.items) {
        that.totalCount = res.items.totalCount;
        that.tableData = res.items.items;
        no ? "" : changeTableHeight(that);
      } else if (res.objectSummaries) {
        that.tableData = res.objectSummaries;
      }
    });
}
export function _sizeChange(val, page, searchInfo) {
  // 传递当前值 分页信息，以及需要copy的对象
  page.pageSize = val;
  const result = {};
  result.page = JSON.parse(JSON.stringify(page));
  Object.assign(result, searchInfo);
  return result;
}
export function getCategory(res) {
  return res.date.filter(v => v.dicName === res.field);
}
export function getDicName(res) {
  const result = res.data.filter(v => v.dicName === res.field);
  let a = "";
  result[0].children.forEach(v => {
    if (v.dicCode === res.dicCode) {
      a = v.dicName;
    }
  });
  return a;
}
export function getDicCode(res) {
  const result = res.data.filter(v => v.dicName === res.field);
  let a = "";
  result[0].children.forEach(v => {
    if (v.dicName === res.dicName) {
      a = v.dicCode;
    }
  });
  return a;
}
export function getInnerDicName(result) {
  let a = "";
  result.data.children.forEach(v => {
    if (v.dicCode === result.dicCode) {
      a = v.dicName;
    }
  });
  return a;
}
export function selfdicCode(result) {
  let a = "";
  result.data.forEach(v => {
    if (v.dicCode === result.dicCode) {
      a = v.dicCode;
    }
  });
  return a;
}
export function createDics(res, select) {
  const obj = {};
  res.forEach(v => {
    obj[v[select]] = v;
  });
  return obj;
}
export function trueOrFalse(res, bool) {
  return new Promise(resolve => {
    res
      .$confirm(
        bool ? "正在执行重置密码, 是否继续" : "正在执行删除操作, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
      .then(() => {
        resolve();
      })
      .catch(() => {
        res.$message({
          type: "info",
          message: bool ? "已取消重置密码" : "已取消删除"
        });
      });
  });
}
export function trueOrFalse1(res, obj) {
  return new Promise((resolve, reject) => {
    res
      .$confirm(obj.confirm, {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
      .then(() => {
        resolve();
      })
      .catch(() => {
        res.$message({
          type: "info",
          message: obj.message
        });
        reject();
      });
  });
}
export function reduceArr(arr, str) {
  arr.forEach(v => {
    if (v.children) {
      if (v.url === str) {
        if (v.permissionCode) {
          result = JSON.parse(JSON.stringify(v));
        }
      }
      reduceArr(v.children, str);
    }
  });
  return result;
}

export function randomRangeId(num) {
  var returnStr = "";
  var charStr =
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  for (var i = 0; i < num; i++) {
    var index = Math.round(Math.random() * (charStr.length - 1));
    returnStr += charStr.substring(index, index + 1);
  }
  return returnStr;
}

function zeroFill(i) {
  if (i >= 0 && i <= 9) {
    return "0" + i;
  } else {
    return i;
  }
}
export function _getCurrentTime() {
  var date = new Date(); // 当前时间
  var month = zeroFill(date.getMonth() + 1); // 月
  var day = zeroFill(date.getDate()); // 日
  var hour = zeroFill(date.getHours()); // 时
  var minute = zeroFill(date.getMinutes()); // 分
  var second = zeroFill(date.getSeconds()); // 秒

  // 当前时间
  var curTime =
    date.getFullYear() +
    "-" +
    month +
    "-" +
    day +
    " " +
    hour +
    ":" +
    minute +
    ":" +
    second;

  return curTime;
}

export function _getformatTimer(second){
  if(second>0){
    if(second < 60) {
      if(second < 10){
        return `00 : 00 : 0${second}`
      }
      return `00 : 00 : ${second}`
    }else if(second < 3600){
      return `00 : ${zeroFill(parseInt(second / 60))} : ${zeroFill(second % 60)}`
    }else {
      return `${zeroFill(parseInt(second / 3600))} : ${zeroFill((parseInt(second / 60))%60)} : ${zeroFill(second % 60)}`
    }
  }else{
    return ''
  }
}